package com.yunqiao.admin.modules.api.controller;

import java.awt.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yunqiao.service.api.entity.RegionEntity;
import com.yunqiao.service.api.RegionService;
import com.yunqiao.common.utils.PageUtils;
import com.yunqiao.common.utils.R;


/**
 * 行政区域表
 *
 * @author Crocutax
 * @email wangxw725@163.com
 * @date 2019-03-08 11:20:27
 */
@RestController
@RequestMapping("api/region")
public class RegionController {

    @Autowired
    private RegionService regionService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("api:region:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = regionService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("api:region:info")
    public R info(@PathVariable("id") Integer id){
        RegionEntity region = regionService.getById(id);
        return R.ok().put("region", region);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("api:region:save")
    public R save(@RequestBody RegionEntity region){
        regionService.save(region);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("api:region:update")
    public R update(@RequestBody RegionEntity region){
        regionService.updateById(region);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("api:region:delete")
    public R delete(@RequestBody Integer[] ids){
        regionService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 获取根据父id获取 省市区
     */
    @RequestMapping("/getLocation")
    public R getLocation(Integer pid){
        List<Object> list = regionService.getLocation(pid);
        return R.objectOk(list);
    }


}
